use rcfs.dta, clear 

*** Figure 1 ***
preserve
gen obs=1

bys year bank: egen tot_obs=total(obs)
bys year bank: egen tot_pay=total(obs) if payer==1
bys year bank: egen totpay=mean(tot_pay)

gen share_payer=(totpay/tot_obs)*100

label var share_payer "Fraction of Payers (dividends)"


twoway (line share_payer year if bank==1,lwidth(thick) lcolor(gs2) lpattern(solid)) (line share_payer year if bank==0, lwidth(thick) lcolor(gs2) lpattern(dash)), title("Percentage of firms paying dividends") ytitle(" ")  xtitle("Year") xlabel(2005(2)2018) legend(order (1 "Financial Firms" 2 "Non-Financial Firms") position(6) ring(1) rowgap(0)) saving(figure1, replace) ylabel(65(10)95)
gr export figure1.png, as(png) replace

bys year bank: egen tot_pp=total(obs) if pp==1
bys year bank: egen totpp=mean(tot_pp)

gen share_pp=(totpp/tot_obs)*100

label var share_payer "Fraction of Payers (dividends + repurchases)"

twoway (line share_pp year if bank==1,lwidth(thick) lcolor(gs2) lpattern(solid)) (line share_pp year if bank==0, lwidth(thick) lcolor(gs2) lpattern(dash)), title("Percentage of firms paying dividends or repurchasing shares") legend(order (1 "Financial Firms" 2 "Non-Financial Firms") position(6) ring(1) rowgap(0)) legend(rows(1)) ytitle(" ") xtitle("Year") xlabel(2005(2)2018) saving(figure2, replace) ylabel(65(10)95)
gr export figure2.png, as(png) replace

restore 

graph combine figure1.gph figure2.gph, rows(2) 
graph export fig1.pdf, as(pdf) replace

***** Table 1 ******
reg payer bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, cluster(s_t)
gen sample=e(sample)
global control "payer pp div_book div_rep_book roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own patient"
estpost tabstat $control if sample, by(bank) statistics(mean sd n) columns(statistics) nototal
est store B
esttab B using table1.tex, main(mean) aux(sd) label replace nostar unstack nonote nomtitle nonumber


***** Table 2 ******
eststo clear
reg payer bank roe lmarketcap mbv lass i.c_t, cluster(s_t)
outreg2 using table2.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Country-Year FE, Y)
reg payer bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, cluster(s_t)
outreg2 using table2.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass i.c_t, cluster(s_t)
outreg2 using table2.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, cluster(s_t)
outreg2 using table2.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Country-Year FE, Y)


***** Table 3 ******
eststo clear
reg payer bank roe lmarketcap mbv lass ret_eq idi sys eq_assets i.c_t, cluster(s_t)
outreg2 using table3.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Adj R2, e(r2_a)) addtext(Country-Year FE, Y)
reg payer bank roe lmarketcap mbv lass ret_eq idi sys bidask i.c_t, cluster(s_t)
outreg2 using table3.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Adj R2, e(r2_a)) addtext(Country-Year FE, Y)
reg payer bank roe lmarketcap mbv lass ret_eq idi sys inst_own i.c_t, cluster(s_t) 
outreg2 using table3.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Adj R2, e(r2_a)) addtext(Country-Year FE, Y)
reg payer bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own i.c_t, cluster(s_t)
outreg2 using table3.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Adj R2, e(r2_a)) addtext(Country-Year FE, Y)

**** Table 4 ***
eststo clear
reg pp bank roe lmarketcap mbv lass ret_eq idi sys eq_assets i.c_t, cluster(s_t)
outreg2 using table4.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys bidask i.c_t, cluster(s_t) 
outreg2 using table4.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys inst_own i.c_t, cluster(s_t)
outreg2 using table4.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own i.c_t, cluster(s_t)
outreg2 using table4.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addtext(Country-Year FE, Y)

***** Table 5 ******
eststo clear
tobit div_book bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, vce(cluster s_t) ll(0)
outreg2 using table5.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)
*eststo: margins, dydx(bank roe lmarketcap mbv lass ret_eq idi sys) predict (e(0,.)) atmeans 
*esttab using table5.tex, replace label beta(a2) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon
tobit div_book bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own i.c_t, vce(cluster s_t) ll(0) 
outreg2 using table5.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)

*eststo:  margins, dydx(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) predict (e(0,.)) atmeans 
*esttab using table5.tex, replace label beta(a2) keep(bank roe lmarketcap mbv lass ret_eq idi sys  eq_assets bidask inst_own) nocon
tobit div_rep_book bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, vce(cluster s_t) ll(0)
outreg2 using table5.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)

*eststo: margins, dydx(bank roe lmarketcap mbv lass ret_eq idi sys) predict (e(0,.)) atmeans 
*esttab using table5.tex, replace label beta(a2) keep(bank roe lmarketcap mbv lass ret_eq idi sys ) nocon
tobit div_rep_book bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own i.c_t, vce(cluster s_t) ll(0)
outreg2 using table5.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)
*eststo: margins, dydx(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) predict (e(0,.)) atmeans 
*esttab using table5.tex, replace label beta(a2) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask inst_own) nocon

**** Table 7 ****
eststo clear
reg payer bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient i.c_t, cluster(s_t)
outreg2 using table7a.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient) nocon addtext(Country-Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient i.c_t, cluster(s_t)
outreg2 using table7a.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient) nocon addtext(Country-Year FE, Y)

tobit div_book bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient i.c_t, vce(cluster s_t) ll(0) 
outreg2 using table7b.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)

tobit div_rep_book bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient i.c_t, vce(cluster s_t) ll(0) 
outreg2 using table7b.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys eq_assets bidask patient) nocon addstat(Pseudo R2, e(r2_p)) addtext(Country-Year FE, Y)

*** Table 6 - matching ***
preserve
collapse (mean) bank eq_assets bidask inst_own, by(id cg sector)

psmatch2 bank eq_assets bidask inst_own, logit

gen pscore2=cg*1000+_pscore
sum pscore2
psmatch2 bank, pscore(pscore2) neighbor(1) caliper(0.01)

gen pair = _id if _treated==0
replace pair = _n1 if _treated==1

bysort pair: egen paircount = count(pair)

keep if paircount>=2

keep id _treated pscore2 cg
sort id 
saveold temp_matched.dta, replace
restore

sort id
merge m:1 id using temp_matched.dta
gen matched=(_merge==3)
drop _merge
drop pscore2

preserve
keep if matched==1
drop s_t
egen s_t=group(sector year)
drop c_t
egen c_t=group(cg year)

reg payer bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, cluster(s_t)
outreg2 using table6a.tex, replace label dec(4) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Year FE, Y)
reg pp bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, cluster(s_t)
outreg2 using table6a.tex, append label dec(4) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addtext(Year FE, Y)

tobit div_book bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, vce(cluster s_t) ll(0)
outreg2 using table6b.tex, replace label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addstat(Pseudo R2, e(r2_p)) addtext(Year FE, Y)
tobit div_rep_book bank roe lmarketcap mbv lass ret_eq idi sys i.c_t, vce(cluster s_t) ll(0)
outreg2 using table6b.tex, append label dec(3) keep(bank roe lmarketcap mbv lass ret_eq idi sys) nocon addstat(Pseudo R2, e(r2_p)) addtext(Year FE, Y)
restore



